//
//  BGHuoPinZhengShuController.m
//  bigMall
//
//  Created by ios on 16/8/4.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGHuoPinZhengShuController.h"
#import "SZAddImage.h"
#import "BGDateResult.h"
#import "BGFileUpLoadModel.h"
#import "BGOwnGoodsDelModel.h"
#import "BGReadAddGoodsStepThreeModel.h"
#import "UIImageView+WebCache.h"
#import "BGBasicUrl.h"
#import "BGGetImageInfoModel.h"
#import "MBProgressHUD.h"
#import "MBProgressHUD+MJ.h"

@interface BGHuoPinZhengShuController () {
    
    NSInteger tag;
    
    UIButton *_btn;
    UIImage *_image;
    NSInteger index;
    Boolean state;
    NSInteger btnIndex;
    NSMutableArray *_array;
    NSMutableArray *imageArr;
    NSMutableString *str;
    NSMutableArray *dataArray;
    UIScrollView *scrolle;
    UIView *vie;
    SZAddImage *a;
    
}

@end

@implementation BGHuoPinZhengShuController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"货品证书";
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"退出" style:UIBarButtonItemStylePlain target:self action:@selector(tuichu)];
    self.navigationItem.rightBarButtonItem = right;
    
    index = 0;
    state = NO;
    tag = 1;
    
    imageArr = [NSMutableArray array];
    
    _array = [NSMutableArray array];
    
    _imageData = [NSMutableArray array];
    
    dataArray= [NSMutableArray array];
    
    [self createUI];
    
    [self judge];
    
    [self loadInter];
    
}
- (void)loadInter {
    
    [MBProgressHUD showMessage:@"加载中..."];
    
    [BGDateResult readAddGoodsStepThreeWith:self.objectId success:^(NSObject *result) {
        
        [MBProgressHUD hideHUD];
        
        BGReadAddGoodsStepThreeModel *model = (BGReadAddGoodsStepThreeModel *)result;
        
//        dataArray = [model.check_report componentsSeparatedByString:@","];
        
        [BGDateResult getImageInfoWithIds:model.check_report success:^(NSObject *result) {
            
            BGGetImageInfoModel *InfoModel = (BGGetImageInfoModel *)result;
            
            for (BGGetImageInfoModelRes *InfoModelRes in InfoModel.res) {
                [dataArray addObject:InfoModelRes.url];
            }
            
            //创建多选按钮啊
            int colCount = 3;
            CGFloat width = ([UIScreen mainScreen].bounds.size.width - 46 - 32) / 3;
            CGFloat heigh = 140;
            CGFloat maginX = 23;
            CGFloat maginY = 16;
            for (int i = 0 ; i < dataArray.count; i++) {
                int row = i / colCount;
                int col = i % colCount;
                CGFloat x = maginX + col * (width + 16);
                CGFloat y = maginY + row * (heigh + maginY);
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, heigh)];
                [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[BGBasicUrl getBasicUrl],dataArray[i]]]];
                [vie addSubview:imageView];
            }
            
        } failure:^(NSError *error) {
            
        }];
        
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:@"加载失败!"];
    }];
    
}
- (void)judge {
    
     if ([self.status isEqualToString:@"0"] || [self.status isEqualToString:@"1"] || [self.status isEqualToString:@"2"]) {
         
         _btn.userInteractionEnabled = NO;
         a.userInteractionEnabled = NO;
         a.hidden = YES;
         vie.hidden = NO;
         _btn.backgroundColor = [UIColor lightGrayColor];
        
         
     }else {
         
         _btn.userInteractionEnabled = YES;
         vie.hidden = YES;
         a.hidden = NO;
         a.userInteractionEnabled = YES;
         _btn.backgroundColor = [UIColor redColor];
         
     }
    
}
- (void)createUI {
    
    scrolle = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrolle.contentSize = CGSizeMake(0, self.view.bounds.size.width * 2.5);
    scrolle.bounces = NO;
    [self.view addSubview:scrolle];
    
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 64, BGWidth, 1)];
    v.backgroundColor = [UIColor lightGrayColor];
    [scrolle addSubview:v];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(23, 17, 100, 30)];
    lab.text = @"货品证书";
    [scrolle addSubview:lab];
    
    _btn = [[UIButton alloc] initWithFrame:CGRectMake(20, self.view.bounds.size.height - 60, self.view.bounds.size.width - 40, 50)];
    
    _btn.clipsToBounds = YES;
    
    _btn.layer.cornerRadius = 10;
    [_btn setTitle:@"保存并发布" forState:UIControlStateNormal];
    [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_btn setBackgroundColor:[UIColor redColor]];
    
    [_btn addTarget:self action:@selector(fabuAndsave) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_btn];
    
    a = [[SZAddImage alloc] initWithFrame:CGRectMake(27, 156 - 64, BGWidth - 27, BGHight - 250)];
    
    [scrolle addSubview:a];
    
    vie = [[UIView alloc] initWithFrame:CGRectMake(0, 156 - 64, BGWidth, BGHight - 250)];
    vie.hidden = YES;
    [scrolle addSubview:vie];
    
}
- (void)fabuAndsave {
    
//    int j = 1;
    
    NSLog(@"[BGHuoPinZhengShuController share].imageData === %@",[BGHuoPinZhengShuController share].imageData);
    
    for (int i = 0; i < [BGHuoPinZhengShuController share].imageData.count; i++) {
        
        if (i == 0) {
              str = [NSMutableString stringWithFormat:@"%@",[BGHuoPinZhengShuController share].imageData[i]];
        }else {
        str = [NSMutableString stringWithFormat:@"%@,%@",str,[BGHuoPinZhengShuController share].imageData[i]];
        }
        
//        if (i != 0 || i != self.imageData.count - 1) {
//            [str insertString:@"," atIndex:j + 3];
//        }
        
    }
    
    if ([BGHuoPinZhengShuController share].imageData.count == 0) {
        str = [NSMutableString stringWithString:@""];
    }
    
    NSLog(@"%@",str);
    NSLog(@"%@",self.objectId);
    
    [BGDateResult addGoodsStepThreeWithObjectId:self.objectId checkREport:str success:^(NSObject *result) {
        
        BGOwnGoodsDelModel *model = (BGOwnGoodsDelModel *)result;
        
        if ([model.status isEqualToString:@"1"]) {
            
            [self showMessage:model.msg];
            
            NSInteger indexaa=[[self.navigationController viewControllers]indexOfObject:self];
            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:indexaa-3]animated:YES];
            
        }else {
            
            [self showMessage:model.msg];
            
        }
        
    } failure:^(NSError *error) {
        
    }];
    
}
- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}
- (void)tuichu {
    
    NSInteger indexaa=[[self.navigationController viewControllers]indexOfObject:self];
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:indexaa-3]animated:YES];
    
}
+ (BGHuoPinZhengShuController *)share {
    
    static BGHuoPinZhengShuController *a = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate,^{
        a = [[self alloc] init];
    });
    return a;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
